﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class DrainOutletReposity : Reposity
    {
        public Drain_system_basic_info GetDrain(int stackId, float El)
        {
            var infos = from d in _dataContext.Drain_system_basic_infos
                        where d.Stack_id == stackId && d.El == El
                        select d;
            if (infos != null && infos.Count() > 0)
            {
                return infos.First();
            }
            else
            {
                return null;
            }
        }
        
        public IEnumerable<Drain_system_basic_info> ListDrains(int stackId)
        {
            var infos = from d in _dataContext.Drain_system_basic_infos
                        where d.Stack_id == stackId
                            select d;
            foreach (Drain_system_basic_info d in infos)
            {
                yield return d;
            }
        }

        public IEnumerable<Drain_system_outlet_info> ListDrainOutlets(int stackId, float El)
        {
            var outlets = from d in _dataContext.Drain_system_outlet_infos
                          where stackId == d.Stack_id && d.El == El
                          select d;
            foreach (Drain_system_outlet_info d in outlets)
            {
                yield return d;
            }
        }

        public Drain_system_basic_info CreateDrain(Drain_system_basic_info drain)
        {
            _dataContext.Drain_system_basic_infos.InsertOnSubmit(drain);
            SubmitChanges();
            return drain;
        }

        public void DeleteDrains(int stackId)
        {
            var drains = from s in _dataContext.Drain_system_basic_infos
                          where s.Stack_id == stackId
                          select s;

            foreach (Drain_system_basic_info drain in drains)
            {
                DeleteOutlets(drain.Stack_id, drain.El);
                _dataContext.Drain_system_basic_infos.DeleteOnSubmit(drain);
            }
            SubmitChanges();
        }

        public void DeleteDrain(int stackId, float El)
        {
            var drains = from s in _dataContext.Drain_system_basic_infos
                         where s.Stack_id == stackId && s.El == El
                         select s;

            foreach (Drain_system_basic_info drain in drains)
            {
                DeleteOutlets(drain.Stack_id, drain.El);
                _dataContext.Drain_system_basic_infos.DeleteOnSubmit(drain);
            }
            SubmitChanges();
        }

        public Drain_system_outlet_info CreateDrainOutlet(Drain_system_outlet_info outlet)
        {
            _dataContext.Drain_system_outlet_infos.InsertOnSubmit(outlet);
            SubmitChanges();
            return outlet;
        }

        private void DeleteOutlets(int stackId, float El)
        {
            var outlets = from s in _dataContext.Drain_system_outlet_infos
                        where s.Stack_id == stackId && s.El == El
                        select s;

            foreach (Drain_system_outlet_info outlet in outlets)
            {
                _dataContext.Drain_system_outlet_infos.DeleteOnSubmit(outlet);
            }
            SubmitChanges();
        }
    }
}